import { IMessageSetup, PLAYER_BLOC } from '../../global/global-constant';
import { DefaultStage } from '../default-stage';
import { AbstractBloc } from './abstract-bloc';
import { SetupPartition } from './setup-partition';
import { CommunityManager } from '../community/community-manager';
import { PropertyUpdater } from './property-updater';
import { PlayerBlock } from './player-bloc';
import { BotBloc } from './bot-bloc';


class BlocManager {

    public static readonly fixed: AbstractBloc[] = [];
    public static readonly other: AbstractBloc[] = [];
    public static self: AbstractBloc;

    private static _stage: DefaultStage;

    public static initialize(stage: DefaultStage, options: IMessageSetup): void {
        console.log('..................', ...options.players)

        this._stage = stage;
        const allPlayerSelected: PLAYER_BLOC[] = [];
        allPlayerSelected.push(options.user.bloc);
        for (const player of options.players) {
            allPlayerSelected.push(player.bloc);
        }
        SetupPartition.setup(allPlayerSelected);
        CommunityManager.updateSovereign();

        this.self = new PlayerBlock(this._stage, {
            userId: options.user.id,
            userName: options.user.name,
            blocName: options.user.bloc,
        });
        for (const player of options.players) {
            this.other.push(new PlayerBlock(this._stage, {
                userId: player.id,
                userName: player.name,
                blocName: player.bloc,
            }));
        }
        for (const bloc of SetupPartition.botBloc) {
            this.fixed.push(new BotBloc(this._stage, {
                blocName: bloc.name
            }));
        }

        this.self.updateSovereign();
        this.other.forEach(player => player.updateSovereign());
        this.fixed.forEach(bloc => bloc.updateSovereign());
        this.self.updatePopulation();
        /**
         * 玩家势力的信息不应该每个客户端自行计算决定
         * 当前的策略：每个客户端都计算所有势力信息并发送给服务器，服务器接收到的第一个数据为用，再下发到每个客户端
         * 数据包括
         * 
         * 或者扩充random组件种子随机数功能，保证所有客户端的生成数据都一致
         * 
         */
        console.log('------------',this.other);

        PropertyUpdater.attach(this.self);
    }

}


export { BlocManager };